package com.seasugar.raggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seasugar.raggie.model.correlation.CategoryDish;
import com.seasugar.raggie.model.dto.DishDto;
import com.seasugar.raggie.model.entity.Dish;
import com.seasugar.raggie.mapper.DishMapper;
import com.seasugar.raggie.model.entity.DishFlavor;
import com.seasugar.raggie.model.vo.DishVo;
import com.seasugar.raggie.service.CategoryDishService;
import com.seasugar.raggie.service.DishFlavorService;
import com.seasugar.raggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private CategoryDishService categoryDishService;
    @Autowired
    private DishFlavorService dishFlavorService;

    //多表操作，添加事务
    @Transactional
    public boolean insertDish(DishDto dishDto) {
        //添加菜品
        boolean a = this.save(dishDto);
        //添加菜品口味
        //获取菜品id
        Long id = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor dishFlavor : flavors) {
            dishFlavor.setDishId(id);
            dishFlavorService.save(dishFlavor);
        }
        //添加菜品关联,防止误删
        boolean b = categoryDishService.save(new CategoryDish(null, dishDto.getCategoryId(), dishDto.getId()));
        return a && b;
    }

    @Override
    @Transactional
    public List<Dish> deleteByIds(List<String> list) {
        //获取菜品下的flavor id并删除
        List<Dish> res=new LinkedList<>();
        for (String s : list) {
            LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper();
            queryWrapper.eq(DishFlavor::getDishId,s);
            List<DishFlavor> dflist = dishFlavorService.list(queryWrapper);
            Dish dish = this.getById(s);
            res.add(dish);
            for (DishFlavor dishFlavor : dflist) {
                dishFlavorService.removeById(dishFlavor.getId());
            }
        }
        //删除外键
        for (String s : list) {
            LambdaQueryWrapper<CategoryDish> queryWrapper=new LambdaQueryWrapper();
            queryWrapper.eq(CategoryDish::getDishId,s);
            CategoryDish categoryDish = categoryDishService.getOne(queryWrapper);
            categoryDishService.removeById(categoryDish.getId());
        }
        //删除菜品
        this.removeByIds(list);

        return res;
    }

    @Override
    public DishVo getDishVoById(Long id) {
        DishVo dishVo=new DishVo();
        Dish dish = this.getById(id);
        BeanUtils.copyProperties(dish,dishVo);
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        if(list!=null){
            dishVo.setFlavors(list);
        }
        return dishVo;
    }


    //多表操作，添加事务
    //Transactional并不起作用，不知道为什么
    @Transactional
    public boolean updateDish(DishDto dishDto) {
        //更新菜品
        boolean a = this.updateById(dishDto);

        //更新菜品口味
        //获取菜品id
        Long id = dishDto.getId();
        //先清除相关的所有口味
        LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,id);
        dishFlavorService.remove(queryWrapper);
        //再添加口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor dishFlavor : flavors) {
            dishFlavor.setDishId(id);
            dishFlavorService.save(dishFlavor);
        }

        //更新菜品关联,防止误删
        Long categoryId = dishDto.getCategoryId();
        LambdaQueryWrapper<CategoryDish> queryWrapper1=new LambdaQueryWrapper();
        queryWrapper1.eq(CategoryDish::getDishId,dishDto.getId());
        CategoryDish categoryDish = categoryDishService.getOne(queryWrapper1);
        boolean b=false;
//        if(categoryId==categoryDish.getCategoryId())
//            b=true;
//        else{
            //消除原来的关联，建立新关联
            categoryDishService.remove(queryWrapper1);
            b = categoryDishService.save(new CategoryDish(null, dishDto.getCategoryId(), dishDto.getId()));
//        }
        return a&&b;
    }

}
